FROM ubuntu:20.04 AS builder

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 使用国内镜像源
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list

# 安装依赖
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-dev \
    && rm -rf /var/lib/apt/lists/*

# 创建工作目录
WORKDIR /app

# 复制依赖文件
COPY requirements.txt .

# 使用国内镜像源安装Python依赖
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir -r requirements.txt --target=/app/python_deps

# 设置环境变量
ENV PYTHONUNBUFFERED=1

# 第二阶段：最终镜像
FROM ubuntu:20.04

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 使用国内镜像源
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list

# 安装依赖
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-dev \
    bpfcc-tools \
    python3-bpfcc \
    build-essential \
    linux-headers-generic \
    git \
    kmod \
    && rm -rf /var/lib/apt/lists/*

# 设置环境变量
ENV PYTHONUNBUFFERED=1

# 添加启动脚本
COPY start.sh /start.sh
RUN chmod +x /start.sh

# 创建工作目录
WORKDIR /app

# 从构建阶段复制Python依赖
COPY --from=builder /app/python_deps /usr/local/lib/python3.8/dist-packages/

# 复制应用代码
COPY src/ ./src/

# 设置环境变量
ENV PYTHONPATH=/app
ENV HOST=0.0.0.0
ENV PORT=5000
ENV DEBUG=False
ENV MONGO_HOST=116.63.51.45
ENV MONGO_PORT=27017
ENV MONGO_DB=admin
ENV MONGO_USERNAME=root
ENV MONGO_PASSWORD=Zmzzmz010627!

# 暴露端口
EXPOSE 5000

# 修改启动命令
ENTRYPOINT ["/start.sh"]
